package com.easy.stock.dao;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.easy.stock.domain.StockInfo;

/**
 * 股票基本信息的存取接口
 * 
 * @author simonlin
 */
public class StockInfoDao extends BaseDao {
	
	public void addStockInfo(StockInfo stockInfo) {
		if(stockInfo==null)
			return;
		getCollection().update("{_id:\""+stockInfo.getId()+"\"}").upsert().with(stockInfo);
	}
	
	public StockInfo getStockInfo(String code) {
		if(code==null || code.length()==0)
			return null;
		
		return getCollection().findOne("{_id:\""+code+"\"}").as(StockInfo.class);
	}
	
	public void deleteStockInfo(String code) {
		if(code==null || code.length()==0)
			return;
		
		getCollection().remove("{_id:\""+code+"\"}");
	}
	
	public int getTotalCount() {
		return (int)getCollection().count();
	}
	
	public List<StockInfo> getStockInfoList(int offset, int limit) {
		Iterable<StockInfo> data = getCollection().find().skip(offset).limit(limit).as(StockInfo.class);
		List<StockInfo> dataList = new ArrayList<StockInfo>();
		Iterator<StockInfo> iter = data.iterator();
		while(iter.hasNext()) {
			dataList.add(iter.next());
		}
		return dataList;
	}
	
	
}
